import { defineStore } from 'pinia';
import type { RouteLocationNormalizedLoaded } from 'vue-router';
import { getMenuList } from '@/api/menu';
import Menu from './interface/menu';
import { HOME } from '@/common/static';
import { goPage } from '@/utils/skip';
/**
 * 菜单管理
 */
const menuStore = defineStore('menuStore', {
	state: (): Menu => {
		return {
			menuList: [], //路由菜单列表
			tabsList: [], //tabs菜单列表,
			keepAliveName: [], //路由缓存列表
			refresh: true,
		};
	},
	actions: {
		/**
		 * 获取路由表
		 */
		async getList() {
			const res = await getMenuList();
			if (res.code == 200) {
				this.menuList = res.data;
				this.initTabs();
			}
		},
		/**
		 * 初始化tabs添加固定按钮
		 */
		initTabs() {
			this.menuList.forEach((item: AuthRoute.Route) => {
				if (item.meta?.affix) {
					this.tabsList = [...this.tabsList, item];
				}
			});
		},
		/**
		 * 添加tabs
		 * @param item  路由对象信息
		 */
		addTabs(item: RouteLocationNormalizedLoaded) {
			if (this.tabsList.some(({ path }) => path === item.path)) return;
			this.tabsList = [
				...this.tabsList,
				{
					path: item.path,
					name: item.name as string,
					meta: item.meta as any,
				},
			];
		},

		/**
		 * 关闭当前tab
		 * @param PathUrl  点击路径
		 * @param activeMenu 当前选中路径
		 */
		closeTabs(PathUrl: string, activeMenu: string) {
			const index = this.tabsList.findIndex(({ path }) => path == PathUrl);
			this.tabsList[index].meta?.isKeepAlive && this.removeKeepAlive(this.tabsList[index].name);
			this.tabsList.splice(index, 1);
			if (activeMenu != PathUrl) return;
			const nextTab = index > 0 ? this.tabsList[index - 1] : this.tabsList[index + 1];
			// 如果存在下一个或前一个tab，则导航到它的路径
			if (nextTab) {
				goPage(nextTab.path);
			} else {
				goPage(HOME); // 如果没有下一个或前一个tab，则导航到其他合适的路径（例如主页）
			}
		},
		/**
		 * 关闭其他
		 * @param PathUrl  点击路径
		 * @param activeMenu 当前选中路径
		 * @param name 组件名称
		 */
		closeOther(PathUrl: string, activeMenu: string, name: string) {
			this.removeKeepAlive(name, true);
			this.tabsList = this.tabsList.filter(({ path, meta }) => path == PathUrl || meta?.affix);
			const index = this.tabsList.findIndex(({ meta }) => !meta?.affix);
			if (index == -1) return goPage(HOME);
			if (activeMenu != this.tabsList[index].path) return goPage(this.tabsList[index].path);
		},
		/**
		 * 关闭两侧
		 * @param PathUrl  点击路径
		 * @param activeMenu 当前选中路径
		 * @param direction 方向
		 */
		closeDirection(PathUrl: string, activeMenu: string, direction: string) {
			const index = this.tabsList.findIndex(({ path }) => path == PathUrl);
			const removeList = this.tabsList
				.slice(direction == 'left' ? 0 : index + 1, direction == 'left' ? index : this.tabsList.length)
				.filter(({ meta }) => !meta?.affix)
				.map(({ path }) => path);
			this.removeMoreKeepAlive(removeList);
			if (removeList.some((path) => path)) {
				this.tabsList = this.tabsList.filter(({ path }) => !removeList.includes(path));
			}
			if (removeList.includes(activeMenu)) return goPage(PathUrl);
		},
		/**
		 * 关闭全部
		 */
		closeAll() {
			this.tabsList = this.tabsList.filter(({ meta }) => meta?.affix);
			this.setKeepAlive();
			goPage(HOME);
		},
		/**
		 * 添加缓存组件
		 * @param name
		 */
		addKeepAlive(name: string) {
			!this.keepAliveName.includes(name) && this.keepAliveName.push(name);
		},
		/**
		 * 移除缓存组件
		 * @param name
		 * @param negation 是否取反
		 */
		removeKeepAlive(name: string, negation: boolean = false) {
			negation
				? (this.keepAliveName = this.keepAliveName.filter((item) => item == name))
				: (this.keepAliveName = this.keepAliveName.filter((item) => item != name));
		},
		/**
		 * 移除多个缓存组件
		 * @param list
		 */
		removeMoreKeepAlive(list: string[]) {
			const filteredItems = this.tabsList.filter((item) => list.includes(item.path)).map(({ name }) => name);
			this.keepAliveName = this.keepAliveName.filter((name: string) => !filteredItems.includes(name));
		},
		/**
		 * 设置缓存组件
		 * @param list
		 */
		setKeepAlive(list: string[] = []) {
			this.keepAliveName = list;
		},
		/**
		 * 修改定义的内容
		 * @param name  字段名称
		 * @param value  更改的值
		 */
		changeValue<T>(name: string, value: T) {
			(this as any)[name] = value;
		},
	},
	/**按需配置数据持久化 */
	persist: {
		/**默认名称为当前store唯一标识 */
		key: 'tabs',
		/**这里建议临时储存sessionStorage 也可写成window.sessionStorage */
		storage: sessionStorage,
		/**默认当前store里的所有变量都持久化 */
		paths: ['tabsList'],
	},
});
export default menuStore;
